package com.example.cardgame


import android.app.Activity
import android.content.Intent
import android.icu.text.SimpleDateFormat
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.provider.MediaStore
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.RadioGroup
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.ui.text.intl.Locale
import androidx.core.content.FileProvider
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import java.io.File
import java.sql.Timestamp
import java.util.Date


private val CAMERA_REQUEST_CODE = 1
private lateinit var imageUri: Uri


class Activity2 : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_2)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        val nameInput: EditText = findViewById(R.id.nameInput)
        val studentIdInput: EditText = findViewById(R.id.studentIdInput)
        val phoneInput: EditText = findViewById(R.id.phoneInput)
        val emailInput: EditText = findViewById(R.id.emailInput)
        val genderGroup: RadioGroup = findViewById(R.id.genderGroup)
        val hobbyReading: CheckBox = findViewById(R.id.hobbyReading)
        val hobbyTraveling: CheckBox = findViewById(R.id.hobbyTraveling)
        val hobbyGaming: CheckBox = findViewById(R.id.hobbyGaming)
        val saveButton: Button = findViewById(R.id.saveButton)

        saveButton.setOnClickListener {

            val name = nameInput.text.toString()
            val studentId = studentIdInput.text.toString()
            val phone = phoneInput.text.toString()
            val email = emailInput.text.toString()

            var gender = ""
            val selectedGenderId = genderGroup.checkedRadioButtonId
            if (selectedGenderId != -1) {
                val genderRadioButton: RadioButton = findViewById(selectedGenderId)
                gender = genderRadioButton.text.toString()
            }

            val hobbies = mutableListOf<String>()
            if (hobbyReading.isChecked) hobbies.add(hobbyReading.text.toString())
            if (hobbyTraveling.isChecked) hobbies.add(hobbyTraveling.text.toString())
            if (hobbyGaming.isChecked) hobbies.add(hobbyGaming.text.toString())


            val resultIntent = Intent().apply {
                putExtra("name", name)
                putExtra("studentId", studentId)
                putExtra("phone", phone)
                putExtra("email", email)
                putExtra("gender", gender)
                putExtra("hobbies", hobbies.toTypedArray())
            }

            setResult(Activity.RESULT_OK, resultIntent)
            finish()
        }


        val cancelButton: Button = findViewById(R.id.cancelButton)
        cancelButton.setOnClickListener {
            setResult(Activity.RESULT_CANCELED)
            finish()
        }

        val takePhotoButton: Button = findViewById(R.id.takephoto)
        takePhotoButton.setOnClickListener {

            val photoFile = createImageFile() // 创建照片文件
            imageUri = FileProvider.getUriForFile(
                this,
                "${packageName}.fileprovider",
                photoFile
            )

            val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE).apply {
                putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
            }

            if (takePictureIntent.resolveActivity(packageManager) != null) {
                startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE)
            }


        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            // 加载图片到 ImageView
            val imageView: ImageView = findViewById(R.id.imageView)
            imageView.setImageURI(imageUri)
        }
    }
    private fun createImageFile(): File {
        // 创建一个图像文件名称
        val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
        val storageDir: File? = getExternalFilesDir(Environment.DIRECTORY_PICTURES)

        val timeStamp_ = ""
        return File.createTempFile(
            "JPEG_$timeStamp_",  /* 前缀 */
            ".jpg",         /* 后缀 */
            storageDir      /* 目录 */
        )
    }
}



